package com.hjwoo.app.views;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hjwoo.app.R;
import com.hjwoo.app.baseviews.BaseAppCompatActivity;
import com.hjwoo.app.been.OrdersObject;
import com.hjwoo.app.net.RequestListener;
import com.hjwoo.app.net.RequestMethod;
import com.hjwoo.app.net.RequestTag;
import com.hjwoo.app.utils.LoanAccessTokenIdKeeper;
import com.hjwoo.app.utils.LoanLog;
import com.hjwoo.app.utils.LoanToast;
import com.hjwoo.app.utils.Utils;

/**
 * Created by renxiaowen on 2016/8/2
 * 预约下户界面
 */
public class AppointmentActivity extends BaseAppCompatActivity implements View.OnClickListener {

    private static final int TIME = 0;//表示返回的时间
    private static final int GETDATE = 1;
    private Button btnBack, appoinment_btn;
    private RelativeLayout appoinment_paupera_rl1, appoinment_paupera_rl2;
    private TextView allTime;
    private Intent intent;
    private ImageButton takeTime, takeDate;
    private String strTime;
    private String description;
    private String ordersSerializable;
    private OrdersObject ordersObject;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appointment_paupera);
        Utils.initTitle("预约下户", this, true);
        appoinment_paupera_rl2 = (RelativeLayout) findViewById(R.id.appoinment_paupera_rl2);
        btnBack = (Button) findViewById(R.id.btnBack);
        allTime = (TextView) findViewById(R.id.allTime);
        appoinment_btn = (Button) findViewById(R.id.appoinment_btn);
        appoinment_paupera_rl1 = (RelativeLayout) findViewById(R.id.appoinment_paupera_rl1);
        takeDate = (ImageButton) findViewById(R.id.ib_take_data);
        takeTime = (ImageButton) findViewById(R.id.ib_take_time);

        btnBack.setOnClickListener(this);
        takeDate.setOnClickListener(this);
        takeTime.setOnClickListener(this);
        appoinment_paupera_rl1.setOnClickListener(this);
        appoinment_paupera_rl2.setOnClickListener(this);
        appoinment_btn.setOnClickListener(this);

        if (getIntent() != null) {
            //判断客户是A类还是B类,getLevel()为空
            //获取传过来的一个订单的所有信息
           ordersObject = (OrdersObject) getIntent().getSerializableExtra("order");
            if (ordersObject != null && ordersObject.getLender() != null && ordersObject.getLender().getLevel() != null
                    && ordersObject.getLender().getLevel().getName() != null){
                    if (ordersObject.getLender().getLevel().getName().equals("A")) {
                        //A类客户
                        LoanLog.e("test", "A");
                    } else {
                        LoanLog.e("test", "B");
                        //B类客户
                        description = ordersObject.getLender().getLevel().getDescription();
                    }
            }

            ordersSerializable = ordersObject.getSerialNumber();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String time = "";
        LoanLog.e("data", requestCode + "");
        if (data != null) {
            switch (resultCode) {
                case 2://btn1传来的值
                    time = data.getStringExtra("btnTime_txt1");
                    //调用方法将格式转换后显示
                    allTime.setText(time);
                    break;
                case 3:
                    time = data.getStringExtra("btnTime_txt2");
                    allTime.setText(time);
                    break;
                case 4:
                    time = data.getStringExtra("btnTime_txt3");
                    allTime.setText(time);
                    break;
                case 5:
                    time = data.getStringExtra("btnTime_txt4");
                    allTime.setText(time);
                    break;
                case 6:
                    time = data.getStringExtra("btnTime_txt5");
                    allTime.setText(time);
                    break;
                case 7:
                    time = data.getStringExtra("btnTime_txt6");
                    allTime.setText(time);
                    break;
                case 8:
                    time = data.getStringExtra("btnTime_txt7");
                    allTime.setText(time);
                    break;
                case 9:
                    time = data.getStringExtra("btnTime_txt8");
                    allTime.setText(time);
                    break;
                case 10:
                    time = data.getStringExtra("btnTime_txt9");
                    allTime.setText(time);
                    break;
                case 11:
                    time = data.getStringExtra("btnTime_txt10");
                    allTime.setText(time);
                    break;
                case 12:
                    time = data.getStringExtra("btnTime_txt11");
                    allTime.setText(time);
                    break;
                case 13:
                    time = data.getStringExtra("btnTime_txt12");
                    allTime.setText(time);
                    break;
                case 14:
                    time = data.getStringExtra("btnTime_txt13");
                    allTime.setText(time);
                    break;
                case 15:
                    time = data.getStringExtra("btnTime_txt14");
                    allTime.setText(time);
                    break;
                case 16:
                    time = data.getStringExtra("btnTime_txt15");
                    allTime.setText(time);
                    break;
                case 17:
                    time = data.getStringExtra("btnTime_txt16");
                    allTime.setText(time);
                    break;
                case 18://other界面传过来的值
                    time = data.getStringExtra("otherDate");
                    allTime.setText(time);
                    break;
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnBack:
                finish();
                break;
            case R.id.ib_take_data://携带资料
                takeDatum();
                break;
            case R.id.ib_take_time://预约时间
                appoinmentTime();
                break;
            case R.id.appoinment_paupera_rl2://预约时间
                appoinmentTime();
                break;
            case R.id.appoinment_paupera_rl1://携带资料
                takeDatum();
                break;
            case R.id.appoinment_btn:
                appoinment_btn.setEnabled(false);
                //截取时间
                strTime = allTime.getText().toString();
                if (TextUtils.isEmpty(strTime)) {
                    LoanToast.showToastShort("请您选择下户时间", this);
                    return;
                }
                //截取年月日
                String[] splitString = strTime.split(" ");
                for (int i = 0; i < splitString.length; i++) {
                    LoanLog.e("时间切割结果", splitString[i]);
                }
                //截取两个时间段
                String yearAndmonth = splitString[0];//2016-08-04
                String beforeTime = splitString[1];//11:00
                String afterTime = splitString[3];//12:00
                //拼接时间字符串  2016-08-09 09:00:00
                final String startTime = yearAndmonth + " " + beforeTime + ":00";
                final String endTime = yearAndmonth + " " + afterTime + ":00";
                RequestMethod.getMethod().appMakeAppointmentByContact(LoanAccessTokenIdKeeper.readAccessToken(AppointmentActivity.this).getAppSessionId(),
                        ordersSerializable , "下户", startTime, endTime, "", new RequestListener() {
                            @Override
                            public void requestSuccess(String tag, String message) {
                                if (tag.equals(RequestTag.APPMAKEAPPOINTMENTBYCONTACT)) { //使用tag来辨别消息分发
                                //跳到成功界面
                                Intent intent = new Intent(AppointmentActivity.this, AppointmentSuccessActivity.class);
                                Bundle mBundle = new Bundle();
                                mBundle.putSerializable("order",ordersObject);//传递某个单子的信息
                                intent.putExtras(mBundle);
                                startActivity(intent);
                                finish();
                                }
                            }
                            @Override
                            public void requestError(String message) {
                                appoinment_btn.setEnabled(true);
                                Toast.makeText(AppointmentActivity.this, "对不起,预约失败,请稍后再试!", Toast.LENGTH_SHORT).show();
                            }
                        }, AppointmentActivity.this);
                break;
        }

    }

    private void appoinmentTime() {
        intent = new Intent(AppointmentActivity.this, AppointmentTimeActivity.class);
        if (!TextUtils.isEmpty(allTime.getText().toString())) {
            intent.putExtra("strTime", allTime.getText().toString());
        }
        startActivityForResult(intent, GETDATE);
    }

    //需要携带资料
    private void takeDatum() {
        intent = new Intent(this, DatumActivity.class);
        intent.putExtra("description", description);
        startActivity(intent);
    }
    //监听手机返回键
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
